﻿using System.Collections.Generic;

//
// Created by Arosy on 2021/11/29.
//

//
// Created by Arosy on 2021/11/29.
//


public class BootClassLoader
{
	//public static string prePath = "/Users/e/Documents/github/JDK/out/production/JDK/"; //路径前缀 ：项目地址
	public static string prePath = UnityEngine.Application.dataPath +"/"; //路径前缀 ：项目地址

    public static SortedDictionary<string, InstanceKlass > allClass = new SortedDictionary<string, InstanceKlass >(); //所有加载完成可以使用的Java类

	public static InstanceKlass loadKlass(string p)
	{
		p=p.append(".class");
		if (GlobalMembers.allClass.ContainsKey(p))
		{ //先判断此类是否加载过
			return GlobalMembers.allClass[p];
		}
		string tempPath = prePath;
		InstanceKlass klass = ClassFileParser.Parser(ClassRead.readByPath(tempPath.append(p))); //读取.class到内存，然后解析它
		MethodInfo cinit = JavaNativeInterface.getMethod(klass, "<clinit>", "()V"); //找到cinit方法,执行静态变量初始化

		GlobalMembers.allClass[p] = klass; //将解析完的klass存起来
		if (cinit != null)
		{
			JavaNativeInterface.callStaticMethod(Threads.curThread, cinit); //执行cinit方法
		}
		return klass;
	}

}